package com.tj.collection.controller.audit;

import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.kly.user.dto.audit.GreetingResp;
import com.kly.user.dto.report.GreetingRequest;
import com.kly.user.enums.ApprovalStatus;
import com.kly.user.service.GreetingManagerService;
import com.tj.collection.bean.ApiResponse;
import com.tj.collection.controller.audit.request.GreetingAuditReq;
import com.tj.collection.controller.audit.request.GreetingReq;
import com.tj.collection.controller.audit.response.GreetingResponse;
import com.tj.collection.controller.audit.response.GreetingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/greeting")
public class GreetingAuditController {

    @Resource
    GreetingManagerService greetingManagerService;

    /**
     * 招呼语列表
     */
    @RequestMapping("/greeting/list")
    public ApiResponse greetingList(@RequestBody GreetingReq req) {

        final GreetingRequest greetingRequest = GreetingRequest.builder().build();
        BeanUtils.copyProperties(req, greetingRequest);

        GreetingResp greetingResp = greetingManagerService.greetingList(greetingRequest, new Page(req.getPageSize(), req.getCurrent()));
        if (greetingResp == null) {
            return new ApiResponse(GreetingResponse.builder().totalNum(0).build());
        }

        final List<GreetingVo> greetingVos = greetingResp.getGreetingDtoList().stream().map(o -> {
            final GreetingVo vo = GreetingVo.builder().build();
            BeanUtils.copyProperties(o, vo);
            vo.setDisableFlag(o.getApprovalStatus() == 1 || o.getApprovalStatus() == 3);
            vo.setModelCode(o.getModelUserCode());
            return vo;
        }).collect(Collectors.toList());

        final GreetingResponse greetingResponse = GreetingResponse.builder().totalNum(greetingResp.getTotalNum()).greetingDtoList(greetingVos).build();

        return new ApiResponse(greetingResponse);
    }

    /**
     * 招呼语审核
     */
    @RequestMapping("/greeting/audit")
    public ApiResponse greetingAudit(@RequestBody GreetingAuditReq req) {
        if (req.getId() == null) {
            return new ApiResponse().setCode(500).setMsg("The id should not be empty");
        }
        if (req.getAuditStatus() == null) {
            return new ApiResponse().setCode(500).setMsg("The approvalStatus should not be empty");
        }

        final Response approvalResult = greetingManagerService.approval(req.getId(), req.getAuditStatus());
        if (approvalResult == null || approvalResult.getCode() != 0) {
            return new ApiResponse().setCode(500).setMsg("Audit Failed");
        }

        return new ApiResponse();
    }




}